﻿using MVCUserInput.Model;
using MVCUserInput.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace MVCUserInput.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(DB.Tasks);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Details(int Coffee)
        {
            ToDo Model = DB.Tasks.FirstOrDefault(t => t.ToDoId == Coffee);  //returns first element of sequence that satisfies condition
            return View(Model);
        }
        public ActionResult TaskComplete(int Coffee)
        {
            DB.Tasks.FirstOrDefault(t => t.ToDoId == Coffee).DateCompleted = DateTime.Now;
            return RedirectToAction("Details", new { Coffee = Coffee });
        }
        public ActionResult TaskUnComplete(int Coffee)
        {
            DB.Tasks.FirstOrDefault(t => t.ToDoId == Coffee).DateCompleted = null;
            return RedirectToAction("Details", new { Coffee = Coffee });
        }
        public ActionResult AddTask()
        {
            return View(new ToDo());
        }
        [HttpPost]
        public ActionResult AddTask(string Task)
        {
            ToDo Model = new ToDo();
            Model.Task = Task;
            Model.DateCreated = DateTime.Now;
            Model.DateCompleted = null;
            Model.ToDoId = DB.Tasks.OrderByDescending(t => t.ToDoId).FirstOrDefault().ToDoId + 1;
            DB.Tasks.Add(Model);
            return RedirectToAction("Index");
        }
    }

}